home *** CD-ROM | disk | FTP | other *** search
/ Cubase Magazine 30 / Issue #30.iso / pc / 2-SOFTWARE / RTEQ 3.0 / SRC / TRACKBAR.CPP < prev   
Encoding:
C/C++ Source or Header  |  2000-09-03  |  11.5 KB  |  361 lines

  1. /**********************************************************************************
  2. *                                                                                 *
  3. *    EQUALIZER.CPP: The main program, the core of this application                *
  4. *                                                                                 *
  5. *    Copyright (C) 2000  Andrei Grecu                                             *
  6. *                                                                                 *
  7. *    This program is free software; you can redistribute it and/or modify         *
  8. *    it under the terms of the GNU General Public License as published by         *
  9. *    the Free Software Foundation; either version 2 of the License, or            *
  10. *    (at your option) any later version.                                          *
  11. *                                                                                 *
  12. *    This program is distributed in the hope that it will be useful,              *
  13. *    but WITHOUT ANY WARRANTY; without even the implied warranty of               *
  14. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                *
  15. *    GNU General Public License for more details.                                 *
  16. *                                                                                 *
  17. *    You should have received a copy of the GNU General Public License            *
  18. *    along with this program; if not, write to the Free Software                  *
  19. *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    *
  20. *                                                                                 *
  21. *    If you have questions, bug reports regarding my code please contact me at:   *
  22. *    andrei.grecu@aon.at                                                          *
  23. *                                                                                 *
  24. *    Home page:                                                                   *
  25. *    http://members.aon.at/grxpage/index.htm                                      *
  26. *                                                                                 *
  27. **********************************************************************************/
  28.  
  29. #include "precomp.h"
  30. #include "resource.h"
  31. #include "misc.h"
  32. #include "fdt.h"
  33. LPVOID                AllocMemory(DWORD dwsize);
  34. #define                FreeMemory(ptr)                 GlobalFree((HGLOBAL)ptr)
  35.  
  36. typedef struct tagRTEQTRACKBAR {
  37.  
  38.     HWND hWnd;
  39. /*    HBITMAP Track, Bar;
  40.     int TrackWidth, TrackHeight, BarWidth, BarHeight;*/
  41.     int pos, maxpos, minpos;
  42.     tagRTEQTRACKBAR *next;
  43.  
  44. } RTEQTRACKBAR;
  45.  
  46. typedef RTEQTRACKBAR * LPRTEQTRACKBAR;
  47.  
  48. HBITMAP Track = 0, Bar;
  49. int TrackWidth, TrackHeight, BarWidth, BarHeight;
  50.  
  51. extern HINSTANCE hInst;                                // current instance
  52.  
  53. LPRTEQTRACKBAR lptrkbar = NULL;
  54.  
  55. ATOM RTEQTrackBarRegisterClass(HINSTANCE hInstance);
  56. LRESULT CALLBACK TrackBarWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  57.  
  58. void DrawTrackBar(HWND hWnd, HDC hdc);
  59. LPRTEQTRACKBAR AppendTrackBar(HWND hWnd);
  60. void RemoveTrackBar(HWND hWnd);
  61. LPRTEQTRACKBAR GetTrackBar(HWND hWnd);
  62.  
  63. //
  64. //  FUNCTION: MyRegisterClass()
  65. //
  66. //  PURPOSE: Registers the window class.
  67. //
  68. //  COMMENTS:
  69. //
  70. //    This function and its usage is only necessary if you want this code
  71. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  72. //    function that was added to Windows 95. It is important to call this function
  73. //    so that the application will get 'well formed' small icons associated
  74. //    with it.
  75. //
  76. ATOM RTEQTrackBarRegisterClass(HINSTANCE hInstance)
  77. {
  78.     WNDCLASSEX wcex;
  79.  
  80.     wcex.cbSize = sizeof(WNDCLASSEX); 
  81.  
  82.     wcex.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;// | CS_SAVEBITS;
  83.     wcex.lpfnWndProc    = (WNDPROC)TrackBarWndProc;
  84.     wcex.cbClsExtra        = 0;
  85.     wcex.cbWndExtra        = 0;
  86.     wcex.hInstance        = hInstance;
  87.     wcex.hIcon            = NULL;
  88.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  89.     wcex.hbrBackground    = CreateSolidBrush(RGB(192, 192, 192));
  90.     wcex.lpszMenuName    = NULL;
  91.     wcex.lpszClassName    = "RTEQTrackBar";
  92.     wcex.hIconSm        = NULL;
  93.  
  94.     return RegisterClassEx(&wcex);
  95. }
  96.  
  97. //
  98. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  99. //
  100. //  PURPOSE:  Processes messages for the main window.
  101. //
  102. //  WM_COMMAND    - process the application menu
  103. //  WM_PAINT    - Paint the main window
  104. //  WM_DESTROY    - post a quit message and return
  105. //
  106. //
  107. LRESULT CALLBACK TrackBarWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  108. {
  109.  
  110.     #define STR_LENGTH 4096
  111.  
  112.     switch (message) 
  113.     {
  114.         case WM_CREATE:
  115.                 {
  116.                     if(!Track) {
  117.                         LPSTR path = (LPSTR) AllocMemory(STR_LENGTH);
  118.                         LPSTR name = (LPSTR) AllocMemory(STR_LENGTH);
  119.                         LPSTR file = (LPSTR) AllocMemory(STR_LENGTH);
  120.     
  121.                         GetCurrentDirectory(STR_LENGTH, path);
  122.                         GetPrivateProfileString("Skin", "Name", "default", name, STR_LENGTH, "EQUALIZER.INI");
  123.                         lstrcat(path, "\\");
  124.                         lstrcat(path, name);
  125.                         lstrcat(path, "\\"); // Syntax "C:\\CurrentDirectory\\name\\"
  126.                         lstrcpy(name, path); // name = path
  127.     
  128.                         GetPrivateProfileString("Skin", "Track", "track.bmp", file, STR_LENGTH, "EQUALIZER.INI");
  129.                         lstrcat(name, file);
  130.                 
  131.                         Track = (HBITMAP) LoadImage(NULL, name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);    
  132.                         if(!Track) {
  133.                             Track = (HBITMAP) LoadImage(hInst, (LPSTR)IDB_TRACK, IMAGE_BITMAP, 0, 0, 0);    
  134.                         }
  135.  
  136.                         lstrcpy(name, path); // name = path
  137.                         GetPrivateProfileString("Skin", "Bar", "bar.bmp", file, STR_LENGTH, "EQUALIZER.INI");
  138.                         lstrcat(name, file);
  139.  
  140.                         Bar = (HBITMAP) LoadImage(NULL, name, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);    
  141.                         if(!Bar) {
  142.                             Bar = (HBITMAP) LoadImage(hInst, (LPSTR)IDB_BAR, IMAGE_BITMAP, 0, 0, 0);    
  143.                         }
  144.                 
  145.                         BITMAP bm;
  146.                         GetObject(Track, sizeof(BITMAP), &bm);
  147.                         TrackWidth = bm.bmWidth;
  148.                         TrackHeight = bm.bmHeight;
  149.                         GetObject(Bar, sizeof(BITMAP), &bm);
  150.                         BarWidth = bm.bmWidth;
  151.                         BarHeight = bm.bmHeight;
  152.  
  153.                         FreeMemory(name);
  154.                         FreeMemory(path);
  155.                         FreeMemory(file);
  156.                     }
  157.  
  158.                     LPRTEQTRACKBAR lpTrackBar = AppendTrackBar(hWnd);
  159.     
  160.                     HDC hdc = GetDC(hWnd);
  161.                     DrawTrackBar(hWnd, hdc);
  162.                     ReleaseDC(hWnd, hdc);
  163.  
  164.                 }
  165.  
  166.             break;
  167.         case WM_SIZE: 
  168.                 {
  169.                     HDC hdc = GetDC(hWnd);
  170.                     DrawTrackBar(hWnd, (HDC)wParam);
  171.                     ReleaseDC(hWnd, hdc);
  172.                 }
  173.             break;
  174.         case WM_ERASEBKGND:
  175.         case WM_PAINT:
  176.                 //if(message == WM_PAINT) DefWindowProc(hWnd, message, wParam, lParam);
  177.                 if(wParam) {
  178.                     HDC hdc = GetDC(hWnd);
  179.                     DrawTrackBar(hWnd, hdc);
  180.                     ReleaseDC(hWnd, hdc);
  181.                 }
  182.                 else return(DefWindowProc(hWnd, message, wParam, lParam));
  183.             break;
  184.         case WM_DESTROY:
  185.                 {
  186.                 /*    LPRTEQTRACKBAR lpTrackBar = GetTrackBar(hWnd);
  187.                     DeleteObject(lpTrackBar->Track);
  188.                     DeleteObject(lpTrackBar->Bar);*/
  189.                     RemoveTrackBar(hWnd);
  190.                 }
  191.             break;
  192.  
  193.         case TBM_GETPOS:
  194.                 {
  195.                     LPRTEQTRACKBAR lpTrackBar = GetTrackBar(hWnd);
  196.                     return(-lpTrackBar->pos);
  197.                 }
  198.         case TBM_SETPOS:
  199.                 {
  200.                     LPRTEQTRACKBAR lpTrackBar = GetTrackBar(hWnd);
  201.                     lpTrackBar->pos = -(LONG)(lParam);
  202.                     if(wParam) {
  203.                         HDC hdc = GetDC(hWnd);
  204.                         SendMessage(hWnd, WM_PAINT, (WPARAM)hdc, 0);
  205.                         ReleaseDC(hWnd, hdc);
  206.                     }
  207.                 }
  208.             break;
  209.         case TBM_SETRANGE:
  210.                 {
  211.                     LPRTEQTRACKBAR lpTrackBar = GetTrackBar(hWnd);
  212.                     lpTrackBar->minpos = HIWORD(lParam);
  213.                     lpTrackBar->maxpos = (signed short)LOWORD(lParam);
  214.                     if(wParam) {
  215.                         HDC hdc = GetDC(hWnd);
  216.                         SendMessage(hWnd, WM_PAINT, (WPARAM)hdc, 0);
  217.                         ReleaseDC(hWnd, hdc);
  218.                     }
  219.                 }
  220.             break;
  221.  
  222.         //////////////////////////////////
  223.         case WM_LBUTTONDOWN:
  224.                 SetCapture(hWnd);
  225.             break;
  226.         case WM_LBUTTONUP:
  227.                 if(GetCapture() == hWnd) ReleaseCapture();
  228.             break;
  229.         case WM_MOUSEMOVE:
  230.             if(wParam && MK_LBUTTON == MK_LBUTTON) {
  231.                 int x = (short)LOWORD(lParam), y = (short)HIWORD(lParam);
  232.  
  233.                 RECT rc;
  234.                 GetClientRect(hWnd, &rc);
  235.  
  236.                 LPRTEQTRACKBAR lpTrackBar = GetTrackBar(hWnd);
  237.                 COEF rel_height = (COEF)rc.right * BarHeight / BarWidth;
  238.                 COEF bar_position = (COEF)y / (COEF)(rc.bottom - rel_height);
  239.                 bar_position = (COEF)lpTrackBar->minpos - bar_position * (2 * abs(lpTrackBar->maxpos));
  240.                 if(bar_position < lpTrackBar->minpos && bar_position > lpTrackBar->maxpos) {
  241.                     lpTrackBar->pos = (INT)bar_position;
  242.                 }
  243.                 else if(bar_position > lpTrackBar->minpos) {
  244.                     lpTrackBar->pos = lpTrackBar->minpos;
  245.                 }
  246.                 else if(bar_position < lpTrackBar->maxpos) {
  247.                     lpTrackBar->pos = lpTrackBar->maxpos;
  248.                 }
  249.                 HDC hdc = GetDC(hWnd);
  250.                 SendMessage(hWnd, WM_PAINT, (WPARAM)hdc, 0);
  251.                 ReleaseDC(hWnd, hdc);
  252.                 SendMessage(GetParent(hWnd), WM_VSCROLL, MAKELONG(0, -lpTrackBar->pos), (LPARAM) hWnd);
  253.             }
  254.             break;
  255.  
  256.         default:
  257.             return DefWindowProc(hWnd, message, wParam, lParam);
  258.    }
  259.    return 0;
  260. }
  261.  
  262. void DrawTrackBar(HWND hWnd, HDC hdc) {
  263.     
  264.     LPRTEQTRACKBAR lpTrackBar = GetTrackBar(hWnd);
  265.  
  266.     // ****** Draw Track ************************************************************
  267.     RECT rc;
  268.     GetClientRect(hWnd, &rc);
  269.  
  270.     HDC hcdc = CreateCompatibleDC(hdc);
  271.  
  272.     SetStretchBltMode(hdc, HALFTONE);
  273.     SetStretchBltMode(hcdc, HALFTONE);
  274.  
  275.     HGDIOBJ saveObj = SelectObject(hcdc, Track);
  276.     StretchBlt(hdc, 0, 0, rc.right, rc.bottom, hcdc, 0, 0, TrackWidth, TrackHeight, SRCCOPY);
  277.     //BitBlt(lpTrackBar->hdc, 0, 0, rc.right, rc.bottom, lpTrackBar->hcdc, 0, 0, SRCCOPY);
  278.     SelectObject(hcdc, saveObj);
  279.  
  280.     saveObj = SelectObject(hcdc, Bar);
  281.  
  282.     COEF rel_height = (COEF)rc.right * BarHeight / BarWidth;
  283.     COEF log_position = (COEF)((COEF)lpTrackBar->minpos - lpTrackBar->pos) / ((COEF)2 * abs(lpTrackBar->maxpos));
  284.     int abs_position = (INT)(log_position * (rc.bottom - rel_height));
  285.     if(abs_position < 0) abs_position = 0;
  286.  
  287.     StretchBlt(hdc, 0, (INT)abs_position, rc.right, (INT)rel_height, hcdc, 0, 0,  (INT)BarWidth, (INT)BarHeight, SRCCOPY);
  288.     //BitBlt(lpTrackBar->hdc, 0, abs_position, rc.right, abs_position + rc.right, lpTrackBar->hcdc, 0, 0, SRCCOPY);
  289.     SelectObject(hcdc, saveObj);
  290.  
  291.     DeleteDC(hcdc);
  292.  
  293.     // ****** Draw Bar ************************************************************
  294. }
  295.  
  296. LPRTEQTRACKBAR AppendTrackBar(HWND hWnd) {
  297.  
  298.     if(!lptrkbar) {
  299.         lptrkbar = (LPRTEQTRACKBAR) AllocMemory(sizeof(RTEQTRACKBAR));
  300.         lptrkbar->hWnd = hWnd;
  301.         lptrkbar->pos = 0;
  302.         lptrkbar->next = NULL;
  303.         return(lptrkbar);
  304.     }
  305.     else {
  306.         LPRTEQTRACKBAR lpHLPtrkbar = lptrkbar;
  307.         while(lpHLPtrkbar->next != NULL) lpHLPtrkbar = lpHLPtrkbar->next;
  308.  
  309.         LPRTEQTRACKBAR lpNEWtrkbar = (LPRTEQTRACKBAR) AllocMemory(sizeof(RTEQTRACKBAR));
  310.         lpNEWtrkbar->hWnd = hWnd;
  311.         lpNEWtrkbar->pos = 0;
  312.         lpNEWtrkbar->maxpos = -100;
  313.         lpNEWtrkbar->minpos = 100;
  314.         lpNEWtrkbar->next = NULL;
  315.  
  316.         lpHLPtrkbar->next = lpNEWtrkbar;
  317.         return(lpNEWtrkbar);
  318.  
  319.     }
  320.  
  321.     return(NULL);
  322.  
  323. }
  324.  
  325. void RemoveTrackBar(HWND hWnd) {
  326.  
  327.     if(lptrkbar != NULL && lptrkbar->next == NULL) {
  328.         FreeMemory(lptrkbar);
  329.     }
  330.     else if(lptrkbar != NULL) {
  331.         LPRTEQTRACKBAR lpHLPtrkbar = lptrkbar->next;
  332.         LPRTEQTRACKBAR lpHLPtrkbar2 = lptrkbar;
  333.         while(lpHLPtrkbar->next != NULL && lpHLPtrkbar->hWnd != hWnd) {
  334.             lpHLPtrkbar = lpHLPtrkbar->next;
  335.             lpHLPtrkbar2 = lpHLPtrkbar2->next;
  336.         }
  337.  
  338.         if(lpHLPtrkbar->next == NULL && lpHLPtrkbar->hWnd != hWnd) return;
  339.  
  340.         lpHLPtrkbar2->next = lpHLPtrkbar->next;
  341.         FreeMemory(lpHLPtrkbar);
  342.     }
  343.  
  344. }
  345.  
  346. LPRTEQTRACKBAR GetTrackBar(HWND hWnd) {
  347.  
  348.     if(lptrkbar) {
  349.         LPRTEQTRACKBAR lpHLPtrkbar = lptrkbar;
  350.         while(lpHLPtrkbar->next != NULL && lpHLPtrkbar->hWnd != hWnd) lpHLPtrkbar = lpHLPtrkbar->next;
  351.  
  352.         if(lpHLPtrkbar->next == NULL && lpHLPtrkbar->hWnd != hWnd) return(NULL);
  353.  
  354.         return(lpHLPtrkbar);
  355.  
  356.     }
  357.  
  358.     return(NULL);
  359.  
  360. }
  361.